package com.example.zzh.controller;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author zzh
 * @since 2023-08-09
 */
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zzh.entity.ZzhRole;
import com.example.zzh.service.ZzhRoleService;
import com.example.zzh.utils.Result;
import com.example.zzh.utils.ResultGenerator;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author zzh
 * @since 2023-07-28
 */
@RestController
@RequestMapping("/zzh-role")
public class ZzhRoleController {
    @Autowired
    private ZzhRoleService zzhRoleService;


    @GetMapping("/list")
    @ApiOperation(value = "查询所有角色(查询)", notes = "查询所有角色信息")
    public Result list(){
        return ResultGenerator.genSuccessResult(zzhRoleService.list());
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询所有角色分页", notes = "分页")
    public Result list(@RequestBody Map<String,Object> params){
        if (StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit"))) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        Page<ZzhRole> page=new Page<>();
        page.setCurrent((int) params.get("page"));
        page.setSize((int) params.get("limit"));
        LambdaQueryWrapper<ZzhRole> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        IPage r=null;
        if(!StringUtils.isEmpty(params.get("name"))){
            lambdaQueryWrapper.like(ZzhRole::getName,params.get("name"));
        }
        lambdaQueryWrapper.in(ZzhRole::getStatus,1,2);
        r=zzhRoleService.pageNames(page,lambdaQueryWrapper);
        return ResultGenerator.genSuccessResult(r.getRecords(),r.getTotal());
    }

    @PutMapping("/updateStatus")
    @ApiOperation(value = "修改角色状态", notes = "修改角色状态")
    public Result updateStatus(@RequestParam int id){
        ZzhRole zzhRole=zzhRoleService.getRoleById(id);
        if(id==1){
            return ResultGenerator.genErrorResult(404,"不能更改超级管理员状态");
        }
        if(!Objects.isNull(zzhRole)){
            if(zzhRole.getStatus()==1){
                zzhRole.setStatus(2);
            }else if(zzhRole.getStatus()==2){
                zzhRole.setStatus(1);
            }
            if(zzhRoleService.updateById(zzhRole)) {
                return ResultGenerator.genSuccessResult();
            }
            return ResultGenerator.genErrorResult(404,"更新不成功");
        }else {
            return ResultGenerator.genErrorResult(404,"更新不成功");
        }
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增角色", notes = "新增角色信息")
    public Result add(@RequestBody ZzhRole zzhRole){
        if(zzhRoleService.save(zzhRole)){
            return ResultGenerator.genSuccessResult();
        }else {
            return ResultGenerator.genErrorResult(404,"新增不成功");
        }
    }

    @PostMapping("/addOrMod")
    @ApiOperation(value = "新增或修改角色", notes = "新增或修改角色信息")
    public Result addOrMod(@RequestBody ZzhRole zzhRole){
        if(zzhRoleService.saveOrUpdate(zzhRole)){
            return ResultGenerator.genSuccessResult();
        }else {
            return ResultGenerator.genErrorResult(404,"新增不成功");
        }
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改角色", notes = "修改角色信息")
    public Result mod(@RequestBody ZzhRole zzhRole){
        if(zzhRole.getId()==1){
            return ResultGenerator.genErrorResult(404,"不能更改超级管理员信息");
        }
        if(zzhRoleService.updateById(zzhRole)){
            return ResultGenerator.genSuccessResult();
        }else {
            return ResultGenerator.genErrorResult(404,"修改不成功");
        }
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除角色", notes = "删除角色信息")
    public Result delete(@RequestParam int id){
        if(id==1){
            return ResultGenerator.genErrorResult(404,"不能删除超级管理员");
        }
        ZzhRole zzhRole=zzhRoleService.getRoleById(id);
        if(!Objects.isNull(zzhRole)){
            zzhRole.setStatus(0);
            if(zzhRoleService.updateById(zzhRole)) {
                return ResultGenerator.genSuccessResult();
            }
            return ResultGenerator.genErrorResult(404,"删除不成功");
        }else {
            return ResultGenerator.genErrorResult(404,"删除不成功");
        }
    }


    @PostMapping("/findeByName")
    @ApiOperation(value = "查询是否重复", notes = "查询是否重复")
    public Result findeByLoginName(@RequestParam String name){
        List list=zzhRoleService.lambdaQuery().eq(ZzhRole::getName,name).list();
        return list.size()>0?ResultGenerator.genSuccessResult(list):ResultGenerator.genFailResult("不重复");
    }

}

